package com.young.mylib.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Random;

/**
 * Created by Young on 2017/9/27.
 */

public class CommonUtils {
    private final static String TAG = "CommonUtils";

    public static String colorInt2String(int color) {
        return String.format("#%X", color);
    }

    public static String getRandColorCode() {
        String r, g, b;
        Random random = new Random();
        r = Integer.toHexString(random.nextInt(256)).toUpperCase();
        g = Integer.toHexString(random.nextInt(256)).toUpperCase();
        b = Integer.toHexString(random.nextInt(256)).toUpperCase();

        r = r.length() == 1 ? "0" + r : r;
        g = g.length() == 1 ? "0" + g : g;
        b = b.length() == 1 ? "0" + b : b;

        return "#" + r + g + b;
    }

    public static boolean hasSdcard() {
        String status = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(status);
    }

    /**
     * 获取apk的版本号 currentVersionCode
     *
     * @param ctx
     * @return
     */
    public static String getAPPVersionCode(Context ctx) {
        String currentVersionCode = "";
        PackageManager manager = ctx.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(ctx.getPackageName(), 0);
            Log.i(TAG, "VersionCode: " + info.versionName);
            currentVersionCode = info.versionName.replace("-debug", "");
            currentVersionCode = currentVersionCode.replace("-release", "");

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return currentVersionCode;
    }

    public static void delete(String dir, FilenameFilter filter) {
        if (TextUtils.isEmpty(dir))
            return;
        File file = new File(dir);
        if (!file.exists())
            return;
        if (file.isFile())
            file.delete();
        if (!file.isDirectory())
            return;

        File[] lists = null;
        if (filter != null)
            lists = file.listFiles(filter);
        else
            lists = file.listFiles();

        if (lists == null)
            return;
        for (File f : lists) {
            if (f.isFile()) {
                f.delete();
            }
        }
    }
//    @SuppressWarnings("rawtypes")
//    public static void copyProperty(CodeListEntity oldUser, CodeListEntity newUser) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
//        //新的class
//        Class newClass = newUser.getClass();
//        //老的class
//        Class oldClass = oldUser.getClass();
//        //该类所有的属性
//        Field[] newFields = newClass.getDeclaredFields();
//        //新的属性
//        Field newField = null;
//        //老的属性
//        Field oldField = null;
//        for(Field f : newFields){
//            //类中的属性名称
//            String fieldName = f.getName();
//            //通过属性名称获取属性
//            newField = newClass.getDeclaredField(fieldName);
//            //获取属性的值时需要设置为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。
//            //值为 false 则指示反射的对象应该实施 Java 语言访问检查。
//            newField.setAccessible(true);
//            //根据属性获取对象上的值
//            Object newObject = newField.get(newUser);
//            //过滤空的属性或者一些默认值
//            if (isContinue(newObject)) {
//                continue;
//            }
//            oldField = oldClass.getDeclaredField(fieldName);
//            oldField.setAccessible(true);
//            oldField.set(oldUser, newObject);
//        }
//    }

    /**
     * 是否跳出本次循环
     *
     * @param object
     * @return true 是 有null或者默认值
     * false 否 有默认值
     * @author 2014-11-6 上午11:37:22
     */
    private static boolean isContinue(Object object) {
        if (object == null || "".equals(object)) {
            return true;
        }
        String valueStr = object.toString();
        if ("0".equals(valueStr) || "0.0".equals(valueStr)) {
            return true;
        }
        return false;
    }

}
